package com.jinglvhuanjing.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

import com.example.x6.serial.SerialPort;
import com.jinglvhuanjing.app.R;
import com.jinglvhuanjing.app.bean.param.ParamLayout2Bean;
import com.jinglvhuanjing.app.utils.Constants;
import com.jinglvhuanjing.app.utils.LOG;
import com.jinglvhuanjing.app.utils.SingletonSerl;
import com.jinglvhuanjing.app.utils.SpUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class ParamSettingRow2View extends LinearLayoutCompat {
    private String TAG = "ParamSettingRow2View";
    public ParamSettingRow2View(@NonNull Context context) {
        super(context);
        this.init(context);
    }

    public ParamSettingRow2View(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public ParamSettingRow2View(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    /*** 分类站所在小区 ***/
    private CustomEdtView flzszxq;
    /*** 串口通信状态 ***/
    private CustomTextView cktxzt;
    /*** 服务器端口 ***/
    private CustomEdtView fwqdk;
    /*** 版本号 ***/
    private CustomEdtView bbh;

    private void init(Context context) {
        setGravity(Gravity.CENTER_VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.view_params_setting_row_2_layout, this, true);
        this.flzszxq = findViewById(R.id.flzszxq);
        this.cktxzt = findViewById(R.id.cktxzt);
        this.fwqdk = findViewById(R.id.fwqdk);
        this.bbh = findViewById(R.id.bbh);

        this.flzszxq.setParamTag("分类站所在小区", Constants.LEFT_TITLE);
        this.cktxzt.setParamTag("串口通信状态", ParamLayout2Bean.PARAM_KEY_CKTXZT);
        this.fwqdk.setParamTag("服务器端口", Constants.SERVER_PORT);
        this.bbh.setParamTag("版本号", Constants.VERSION);

        initData();
    }

    private void initData(){
        // 分类站所在小区
        flzszxq.setText("" + SpUtils.getString(getContext(), Constants.LEFT_TITLE));
        // 串口通信状态
        try {
            SerialPort serl = SingletonSerl.getInstance().getSerialPortConnect3();
            cktxzt.setText(""+(serl!=null?"正常":"异常"));
        } catch (Exception e) {
            e.printStackTrace();
            LOG.e(TAG, "串口连接错误："+e.toString());
        }
        // 服务器端口
        fwqdk.setText("" + SpUtils.getString(getContext(), Constants.SERVER_PORT));
        // 版本号
        bbh.setText("" + SpUtils.getString(getContext(), Constants.VERSION));
    }

    /**
     * 获取值
     *
     * @return 集合
     */
    public ArrayList<HashMap<String, String>> getFormValue() {
        ArrayList<HashMap<String, String>> values = new ArrayList<>();
        values.add(this.flzszxq.getValueMap());
        values.add(this.cktxzt.getValueMap());
        values.add(this.fwqdk.getValueMap());
        values.add(this.bbh.getValueMap());
        return values;
    }
}
